package cn.demoncat.util.es.entity;

import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.cluster.health.ClusterIndexHealth;

/**
 * 集群信息
 * 
 * @author 延晓磊
 *
 * @since 2019年12月27日
 */
public class ClusterInfo {

	// 名称
	private String name;
	// 状态： GREEN、YELLOW、RED
	private String status;

	// 节点数
	private Integer numberOfNodes;
	// 数据节点数
	private Integer numberOfDataNodes ;
	
	// 活跃的分片(主分片+副本分片)
	private Integer activeShards;
	// 活跃的主分片
	private Integer activePrimaryShards;
	// 活跃的分片占比(%值)
	private Double activeShardsPercent;
	// 迁移中的分片
	private Integer relocatingShards;
	// 初始化中的分片
	private Integer initializingShards;
	// 未分配的分片
	private Integer unassignedShards;

	// 索引列表
	private List<IndexInfo> indexes = new ArrayList<>();
	// 节点列表
	private List<NodeInfo> nodes;

	/**
	 * 转换
	 * 
	 * @param response
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2019年12月27日
	 */
	public static ClusterInfo parse(ClusterHealthResponse response) {
		ClusterInfo cluster = new ClusterInfo();
		// 集群名称
		cluster.setName(response.getClusterName());
		// 集群状态
		cluster.setStatus(response.getStatus().name());
		// 集群节点
		cluster.setNumberOfNodes(response.getNumberOfNodes());
		cluster.setNumberOfDataNodes(response.getNumberOfDataNodes());
		// 集群分片
		cluster.setActiveShards(response.getActiveShards());
		cluster.setActivePrimaryShards(response.getActivePrimaryShards());
		cluster.setActiveShardsPercent(response.getActiveShardsPercent());
		cluster.setRelocatingShards(response.getRelocatingShards());
		cluster.setInitializingShards(response.getInitializingShards());
		cluster.setUnassignedShards(response.getUnassignedShards());
		// 索引状态
		for (ClusterIndexHealth index : response.getIndices().values()) {
			cluster.getIndexes().add(IndexInfo.parse(index));
		}
		return cluster;
	}

	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getActiveShards() {
		return activeShards;
	}

	public void setActiveShards(Integer activeShards) {
		this.activeShards = activeShards;
	}

	public Integer getActivePrimaryShards() {
		return activePrimaryShards;
	}

	public void setActivePrimaryShards(Integer activePrimaryShards) {
		this.activePrimaryShards = activePrimaryShards;
	}

	public Double getActiveShardsPercent() {
		return activeShardsPercent;
	}

	public void setActiveShardsPercent(Double activeShardsPercent) {
		this.activeShardsPercent = activeShardsPercent;
	}

	public Integer getNumberOfNodes() {
		return numberOfNodes;
	}

	public void setNumberOfNodes(Integer numberOfNodes) {
		this.numberOfNodes = numberOfNodes;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<NodeInfo> getNodes() {
		return nodes;
	}

	public void setNodes(List<NodeInfo> nodes) {
		this.nodes = nodes;
	}


	public Integer getNumberOfDataNodes() {
		return numberOfDataNodes;
	}


	public void setNumberOfDataNodes(Integer numberOfDataNodes) {
		this.numberOfDataNodes = numberOfDataNodes;
	}


	public Integer getRelocatingShards() {
		return relocatingShards;
	}


	public void setRelocatingShards(Integer relocatingShards) {
		this.relocatingShards = relocatingShards;
	}


	public Integer getInitializingShards() {
		return initializingShards;
	}


	public void setInitializingShards(Integer initializingShards) {
		this.initializingShards = initializingShards;
	}


	public Integer getUnassignedShards() {
		return unassignedShards;
	}


	public void setUnassignedShards(Integer unassignedShards) {
		this.unassignedShards = unassignedShards;
	}


	public List<IndexInfo> getIndexes() {
		return indexes;
	}


	public void setIndexes(List<IndexInfo> indexes) {
		this.indexes = indexes;
	}

}
